package com.ls.bpmn.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ls.bpmn.dto.*;
import com.ls.bpmn.from.TaskWorkQueryForm;
import com.ls.bpmn.service.IProcessService;
import com.ls.common.Constast;
import com.ls.common.ResultObj;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author lisheng
 * @Date 2025/05/07 01:05
 **/
@RestController
@RequestMapping("bpmn/process")
public class ProcessController {
    private static final Logger log = LoggerFactory.getLogger(ProcessController.class);

    @Autowired
    private IProcessService processService;

    /**
     * 流程启动接口
     * @param dto
     * @return
     */
    @PostMapping("/start")
    public ResultObj statr(@RequestBody ProcessStartDTO dto){
        try{
            dto.validate();
            StartReturnDTO data = processService.start(dto);
            return new ResultObj(Constast.OK,"流程启动成功！",data);
        }catch(Exception e){
            return new ResultObj(Constast.ERROR,"抱歉，流程启动失败！",e.getMessage());
        }
    }

    /**
     * 流程提交接口
     * @param dto
     * @return
     */
    public ResultObj submit(@RequestBody ProcessSubmitDTO dto){
        try{
            dto.validate();
            SubmitReturnDTO data = processService.submit(dto);
            return new ResultObj(Constast.OK,"流程提交成功",data);
        }catch (Exception e){
            return new ResultObj(Constast.ERROR,"抱歉，流程提交失败！"+e.getMessage());
        }
    }

    /**
     * 获取流转记录接口
     * @param processInstanceId
     * @param parentActInstId
     * @return
     */
    @GetMapping("/activity-instance/{processInstanceId}")
    public ResultObj getActivityInstance(
            @PathVariable String processInstanceId,
            @RequestParam(required = false) String parentActInstId){
        try {
            List<HistoricActivityInstanceDTO> list = this.processService.findActivityInstanceDTO(processInstanceId, parentActInstId);
            return new ResultObj(Constast.OK,"操作成功",list);
        } catch (Exception e) {
            log.error("获取工单轨迹异常：{}",e);
            return new ResultObj(Constast.ERROR,"抱歉，操作失败！"+e.getMessage());
        }
    }

    /**
     * 获取全部工单列表
     * @param form
     * @return
     */
    @PostMapping("/order-list")
    public ResultObj getWorkOrder(@RequestBody TaskWorkQueryForm form){
        try {
            Page<TaskListDTO> page = this.processService.findWorkOrder(form);
            List<TaskListDTO> list = page.getRecords();
            return new ResultObj(Constast.OK,"操作成功",list);
        } catch (Exception e) {
            log.error("获取全部工单异常：{}",e);
            return new ResultObj(Constast.ERROR,"抱歉，操作失败！"+e.getMessage());
        }
    }
}
